package com.template.common.lang;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {

    private Integer code;
    private T data;
    private String message;

    public static final int FAIL_CODE = 0;
    public static final int SUCCESS_CODE = 1;
    public static final int LOGIN_CODE = 2; // 用户需重新登录
    public static final int PERMS_CODE = 3; // 用户权限不足

    public static R<String> success() {
        return success(null);
    }

    public static <T> R<T> success(T data) {
        return new R<>(SUCCESS_CODE, data, null);
    }

    public static R<String> fail(String msg) {
        return new R<>(FAIL_CODE, null, msg);
    }

    public static R<String> login(String msg) {
        return new R<>(LOGIN_CODE, null, msg);
    }

    public static R<String> noPerms() {
        return noPerms("权限不足");
    }

    public static R<String> noPerms(String msg) {
        return new R<>(PERMS_CODE, null, msg);
    }
}
